package com.maoyan.quickdevelop.system.service.Impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.maoyan.quickdevelop.common.constant.HttpStatus;
import com.maoyan.quickdevelop.common.core.domain.DqInterview;
import com.maoyan.quickdevelop.common.core.domain.DqType;
import com.maoyan.quickdevelop.common.core.domain.DqUser;
import com.maoyan.quickdevelop.common.core.domain.postprocessor.DqInterviewPostProcesser;
import com.maoyan.quickdevelop.common.core.exception.CustomException;
import com.maoyan.quickdevelop.common.utils.DateUtils;
import com.maoyan.quickdevelop.common.utils.MyQueryWrapper;
import com.maoyan.quickdevelop.common.utils.StringUtils;
import com.maoyan.quickdevelop.system.mapper.DqInterviewMapper;
import com.maoyan.quickdevelop.system.mapper.DqTypeMapper;
import com.maoyan.quickdevelop.system.mapper.DqUserMapper;
import com.maoyan.quickdevelop.system.mapper.postprocessor.DqInterviewPostProcessorMapper;
import com.maoyan.quickdevelop.system.service.IDqInterviewService;
import com.maoyan.quickdevelop.system.service.IDqInterviewService;
import com.maoyan.quickdevelop.system.service.IDqUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 猫颜
 * @date 2021/5/27 22:28
 * 状态校验的方法先islogin，再判断权限或身份
 */
@Transactional
@Service
public class IDqInterviewServiceImpl implements IDqInterviewService {

    QueryWrapper<DqInterview> queryWrapper = new QueryWrapper<>();
    @Autowired
    private DqInterviewMapper dqInterviewMapper;
    @Autowired
    private DqInterviewPostProcessorMapper dqInterviewPostProcessorMapper;
    @Autowired
    private IDqUserService idqUserService;
    @Autowired
    private DqUserMapper dqUserMapper;

    /**
     * 统一的查询方法
     *
     * @param dqInterview
     * @return
     */
    @Override
    public List<DqInterview> selectDqInterview(int pageNum, int pageSize, DqInterview dqInterview) {
        PageHelper.startPage(pageNum, pageSize);
        queryWrapper.lambda().eq(StringUtils.isNotNull(dqInterview.getInterId()), DqInterview::getInterId, dqInterview.getInterId())
                .like(StringUtils.isNotNull(dqInterview.getInterTitle()), DqInterview::getInterTitle, dqInterview.getInterTitle())
                .eq(StringUtils.isNotNull(dqInterview.getAuthorId()), DqInterview::getAuthorId, dqInterview.getAuthorId())
                .eq(StringUtils.isNotNull(dqInterview.getAuthorNickname()), DqInterview::getAuthorNickname, dqInterview.getAuthorNickname())
                .eq(StringUtils.isNotNull(dqInterview.getAuthorUsername()), DqInterview::getAuthorUsername, dqInterview.getAuthorUsername());
        List<DqInterview> dqInterviews = dqInterviewMapper.selectList(queryWrapper);
        queryWrapper.clear();
        if (dqInterviews.isEmpty()) {
            throw new CustomException("未查询到文章", HttpStatus.NOT_FOUND);
        }
        return dqInterviews;
    }

    @Override
    public DqInterview selectDqInterviewById(Long dqInterviewId) {
        queryWrapper.eq("inter_id", dqInterviewId);
        DqInterview dqInterview = dqInterviewMapper.selectOne(queryWrapper);
        queryWrapper.clear();
        if (dqInterview == null) {
            throw new CustomException("未查询到此文章", HttpStatus.NOT_FOUND);
        }
        return dqInterview;
    }

    @Override
    public DqInterview selectDqInterviewByTitle(String title) {
        MyQueryWrapper<DqInterview> myQueryWrapper = new MyQueryWrapper<>();
        myQueryWrapper.statuseq("inter_title", title);
        DqInterview dqInterview = dqInterviewMapper.selectOne(myQueryWrapper);
        myQueryWrapper.clear();
        if (dqInterview == null) {
            throw new CustomException("未查询到此文章", HttpStatus.NOT_FOUND);
        }
        return dqInterview;
    }

    @Override
    public List<DqInterview> selectDqInterviewsByAuthorId(int pageNum, int pageSize, Long authorId) {
        /** 对authorid校验,将Long转换为String **/
        MyQueryWrapper<DqInterview> myQueryWrapper = new MyQueryWrapper<>();
        myQueryWrapper.eq("author_id", String.valueOf(authorId));
        DqUser dqUser = idqUserService.selectDqUserById(authorId);
        if (StringUtils.isNull(dqUser)) {
            throw new CustomException("此用户不存在", HttpStatus.NOT_FOUND);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<DqInterview> dqInterviews = dqInterviewMapper.selectList(myQueryWrapper);
        //清除queryWrapper的数据，防止给其他的带来影响
        myQueryWrapper.clear();
        return dqInterviews;
    }

    @Override
    public List<DqInterview> selectDqInterviewsByAuthorUsername(int pageNum, int pageSize, String authorUsername) {
        /** 对authornickname校验 **/
        DqUser dqUser = idqUserService.selectDqUserByUserName(authorUsername);
        if (StringUtils.isNull(dqUser)) {
            throw new CustomException("此用户不存在", HttpStatus.NOT_FOUND);
        }
        PageHelper.startPage(pageNum, pageSize);
        MyQueryWrapper<DqInterview> myQueryWrapper = new MyQueryWrapper<>();
        myQueryWrapper.statuseq("author_username", authorUsername);
        List<DqInterview> dqInterviews = dqInterviewMapper.selectList(myQueryWrapper);
        //清除queryWrapper的数据，防止给其他的带来影响
        myQueryWrapper.clear();
        if (dqInterviews.isEmpty()) {
            throw new CustomException("此用户没有文章", HttpStatus.NOT_FOUND);
        }

        return dqInterviews;
    }


    @Override
    public int insertDqInterview(DqInterview dqInterview) {
        Long autorId = dqInterview.getAuthorId();
        DqUser dqUser = dqUserMapper.selectById(autorId);
//        DqUser dqUser = idqUserService.selectDqUserById(autorId);
        dqInterview.setAuthorUsername(dqUser.getUserName());
        dqInterview.setAuthorNickname(dqUser.getNickName());
        int insert = dqInterviewMapper.insert(dqInterview);
        if (insert <= 0) {
            throw new CustomException("添加失败", HttpStatus.ERROR);
        }
        return insert;
    }

    @Override
    public int updateDqInterview(DqInterview newdqInterview) {
        Long articleId = newdqInterview.getInterId();
        DqInterview dqInterview = dqInterviewMapper.selectById(articleId);
        if (dqInterview == null) {
            throw new CustomException("没有此职位", HttpStatus.NOT_FOUND);
        }
        if (StpUtil.getLoginIdAsLong() != dqInterview.getAuthorId()) {
            throw new CustomException("不能更新其他人的职位", HttpStatus.ERROR);
        }
        dqInterview.setInterTitle(newdqInterview.getInterTitle());
        dqInterview.setInterInfo(newdqInterview.getInterInfo());
        dqInterview.setInterCategory(newdqInterview.getInterCategory());
        dqInterview.setInterPhone(newdqInterview.getInterPhone());
        dqInterview.setInterImage(newdqInterview.getInterImage());
        dqInterview.setUpdateTime(DateUtils.getNowDate());
        dqInterview.setInterAddress(newdqInterview.getInterAddress());
        dqInterview.setInterMoney(newdqInterview.getInterMoney());
        dqInterview.setInterType(newdqInterview.getInterType());
        dqInterview.setInterTechnology(newdqInterview.getInterTechnology());
        int i = dqInterviewMapper.updateById(dqInterview);
        if (i <= 0) {
            throw new CustomException("更新失败", HttpStatus.ERROR);
        }
        return i;
    }

    @Override
    public int deleteDqInterviewById(Long dqInterviewId) {
        DqInterview dqInterview = dqInterviewMapper.selectById(dqInterviewId);
        if (dqInterview == null) {
            throw new CustomException("未查询到此文章", HttpStatus.NOT_FOUND);
        }
        Long autorId = dqInterview.getAuthorId();
        if (StpUtil.getLoginIdAsLong() != autorId) {
            throw new CustomException("不能删除其他人的文章", HttpStatus.ERROR);
        }
        int i = dqInterviewMapper.deleteById(dqInterviewId);
        if (i > 0) {
            return i;
        } else {
            throw new CustomException("删除失败", HttpStatus.ERROR);
        }
    }


    @Override
    public List<DqInterviewPostProcesser> selectDqInterviewPostProcessers(int pageNum, int pageSize, DqInterviewPostProcesser dqInterviewPostProcesser) {
        PageHelper.startPage(pageNum, pageSize);
        List<DqInterviewPostProcesser> dqInterviewPostProcessers = dqInterviewPostProcessorMapper.selectAllDqInterviewPostProcesser(dqInterviewPostProcesser);
//        if (dqInterviewPostProcessers.isEmpty()) {
//            throw new CustomException("未查询到文章", HttpStatus.NOT_FOUND);
//        }
        return dqInterviewPostProcessers;
    }

    @Override
    public Integer selectDqInterviewByType(Long typeId) {
        MyQueryWrapper<DqInterview> myQueryWrapper = new MyQueryWrapper<>();
        myQueryWrapper.eq("inter_type", typeId);
        Integer num = dqInterviewMapper.selectCount(myQueryWrapper);
        return num == null ? 0 : num;
    }

    @Override
    public Integer selectDqInterviewByMoney(int startMoney, int overMoney) {
        MyQueryWrapper<DqInterview> myQueryWrapper = new MyQueryWrapper<>();
        myQueryWrapper.ge("inter_money", startMoney);
        if (overMoney > 0) {
            myQueryWrapper.lt("inter_money", overMoney);
        }
        Integer num = dqInterviewMapper.selectCount(myQueryWrapper);
        return num == null ? 0 : num;
    }

    @Override
    public List<String> selectDqInterviewAddress() {
        List<DqInterview> dqInterviews = dqInterviewMapper.selectList(new MyQueryWrapper<>());
        return dqInterviews.stream().map(DqInterview::getInterAddress).distinct().collect(Collectors.toList());
    }

    @Override
    public Integer selectDqInterviewByAddress(String s) {
        MyQueryWrapper<DqInterview> myQueryWrapper = new MyQueryWrapper<>();
        myQueryWrapper.eq("inter_address", s);
        Integer num = dqInterviewMapper.selectCount(myQueryWrapper);
        return num == null ? 0 : num;
    }

}

